##直接图表
from utils.getDataFromMysql import *
from utils.expand import *


@timecost
def generateTable(result_list:list):
    ## 表头数据集合
    columns = []
    tableData = []
    json_result = {}

    if (len(result_list) != 0):
        attributes = list(result_list[0].__dict__.keys())
        ##构建column对象属性列表
        for attribute in attributes:
            tmp = {}
            tmp['prop']=attribute
            tmp['label'] = attribute
            columns.append(tmp)
        ##构建tableData属性列表
        for result_tmp in result_list:
            tableData.append(result_tmp.__dict__)

    json_result['columns'] = columns
    json_result['tableData'] = tableData
    return json.dumps(json_result)

if __name__ == '__main__':
    sql_query = "SELECT year, country, norm, data_value, units FROM weo_data WHERE lower(country) = 'china' AND norm IN ('NGDP_R', 'NGDP_RPCH', 'NGDP', 'NGDPD', 'PPPGDP', 'NGDP_D', 'NGDPRPC', 'NGDPRPPPPC', 'NGDPPC', 'NGDPDPC', 'PPPPC', 'NGAP_NPGDP', 'PPPSH', 'PPPEX', 'NID_NGDP', 'NGSD_NGDP', 'PCPI', 'PCPIPCH', 'PCPIE', 'PCPIEPCH', 'TM_RPCH', 'TMG_RPCH', 'TX_RPCH', 'TXG_RPCH', 'LUR', 'LE', 'LP', 'GGR', 'GGR_NGDP', 'GGX', 'GGX_NGDP', 'GGXCNL', 'GGXCNL_NGDP', 'GGSB', 'GGSB_NPGDP', 'GGXONLB', 'GGXONLB_NGDP', 'GGXWDN', 'GGXWDN_NGDP', 'GGXWDG', 'GGXWDG_NGDP', 'NGDP_FY', 'BCA', 'BCA_NGDPD')"
    result_list = getDataBySql(sql_query)
    response = generateTable(result_list)
    print(response)












